Manually transforming source code without changing the semantics is a challenging task. Building an automated tool that transforms source code correctly is even more difficult. One of the reasons that programmatically transforming code correctly is so difficult is because it is so context-dependent. Suppose for example, a variable name is to be changed from “x” to “y”. Simply doing a “replace all ‘x’s with ‘y’s” is just not going to work. In addition to changing any variable with an ‘x’ in the name, ‘x’s in different code modules may be completely unrelated variables.
One common reason to transform source code is to refactor it. Refactoring has been defined by Martin Fowler as a “disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior”. (See Martin Fowler in http://refactoring.com). Refactoring is typically undertaken in order to improve nonfunctional, dysfunctional or sub-optimal characteristics of the software being altered. Refactoring can result in improved code readability and reduced complexity, allowing refactored source code to be maintained more easily.
Refactoring is often accomplished by applying a series of small changes in source code and checking to make sure that the change either preserves desired behavior of the software or retains its functionality.